package com.example.bob.uicontrols;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

/**
 * Created by Bob on 2016/3/12.
 */
public class AtyUsingImageSwitcher extends Activity{

    private ImageSwitcher imageSwitcher;
    private boolean showImage1 = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_using_imageswitcher);

        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return new ImageView(AtyUsingImageSwitcher.this);
            }
        });
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(AtyUsingImageSwitcher.this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(AtyUsingImageSwitcher.this,android.R.anim.fade_out));

        imageSwitcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showImage1 =!showImage1;

                showCurrentImage();
            }
        });

        showCurrentImage();
    }
    private void showCurrentImage(){
        if (showImage1){
            imageSwitcher.setImageResource(R.drawable.img1);
        }else {
            imageSwitcher.setImageResource(R.drawable.img2);
        }
    }
}
